package com.wpx.Service.Impl;


import com.wpx.Mapper.ArticleMapper;
import com.wpx.Mapper.ParagraphMapper;
import com.wpx.Mapper.WordMapper;
import com.wpx.Pojo.*;
import com.wpx.Service.ArticleService;
import com.wpx.Util.PinYinUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class ArticleServiceImpl implements ArticleService {
    @Autowired
    public ArticleMapper articleMappering;
   @Autowired
   public WordMapper wordMapper;
   @Autowired
   public ParagraphMapper paragraphMapper;
    /**
     * 获取列表
     * @param pageVO
     * @return
     */
    @Override
    public List<Article> getArticleList(PageVO pageVO) {
        if (pageVO.getPageNo() == null || pageVO.getPageNo() < 1) {
            pageVO.setPageNo(1); // 默认第一页
        }
        if (pageVO.getPageSize() == null || pageVO.getPageSize() > 100) {
            pageVO.setPageSize(10); // 默认每页10条
        }

        int num =(pageVO.getPageNo()-1)* pageVO.getPageSize();
        pageVO.setPageNo(num);
        return  articleMappering.getArticleList(pageVO);
    }

    /**
     * 获取列表总数
     * @return
     */
    @Override
    public Integer getArticleTotal(PageVO pageVO) {

         return  articleMappering.getArticleTotal(pageVO);
    }

    @Override
    public void updateStatus(Integer id, Integer status) {
        if(status==1){
            status=0;
        }else{
            status=1;
        }
        articleMappering.updateStatus(id,status);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addArticle( ArticleVo articleVo) {

        Article article=new Article();
        article.setUpdateTime(LocalDateTime.now());
        article.setCreateTime(LocalDateTime.now());
        article.setFun(articleVo.getFun());
        article.setType(articleVo.getType());
        article.setStatus(0);
        article.setTitle(articleVo.getTitle());
        articleMappering.saveArticle(article);

        String content = articleVo.getContent();
        String[] split = content.split("\n");
        List<String>sList=new ArrayList<>();
        for (String string : split) {
            if(string==null ||string.equals("")){
                continue;
            }
            sList.add(string);
        }

        for (String string : sList) {
            String replace = string.replace(" ", "");
            Paragraph paragraph =new Paragraph();
            paragraph.setArticleId(article.getId());
            paragraph.setCreateTime(LocalDateTime.now());
            paragraph.setUpdateTime(LocalDateTime.now());
            paragraph.setContent(replace);
            paragraphMapper.saveParagraph(paragraph);
            for(int i=0;i<string.length();i++){
                char c = string.charAt(i);
                if(c==' '){
                  //如果是空格，则去掉
                    continue;
                }
                Word w=new Word();
                w.setWord(c+"");
                w.setIsBold(0);
                w.setParagraphId(paragraph.getId());
                String[] fullPinyin = PinYinUtil.getFullPinyin(c);
                if(fullPinyin.length>1){
                    //代表是多音字
                    w.setIsPolyphone(1);
                    w.setPinyin(fullPinyin[0]);
                }
                if(fullPinyin.length==0){
                    //代表是拼音
                    w.setIsPolyphone(0);
                }
                if(fullPinyin.length==1){
                     w.setIsPolyphone(0);
                     w.setPinyin(fullPinyin[0]);
                }
               wordMapper.saveWord(w);
            }
        }





    }
}
